package org.hamster.multidropbox.mainView.view
{
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	
	import org.hamster.common.puremvc.mediator.BaseMediator;
	import org.hamster.common.services.LoadingMaskService;
	import org.hamster.dropbox.DropboxEvent;
	import org.hamster.dropbox.models.DropboxFile;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxFile;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxLinkFolderItemVO;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxLinkFoldersVO;
	import org.hamster.multidropbox.main.services.AppTipService;
	import org.hamster.multidropbox.main.utils.AppUtil;
	import org.hamster.multidropbox.main.utils.PureMVCHelper;
	import org.hamster.multidropbox.mainView.event.AppFileEvent;
	import org.hamster.multidropbox.mainView.event.MainViewEvent;
	import org.hamster.multidropbox.mainView.model.vo.LinkFoldersStorageStatusVO;
	
	public class LinkFoldersViewMediator extends BaseMediator
	{
		public static const NAME:String = "org.hamster.multidropbox.mainView.view.LinkFoldersViewMediator";
		
		public function LinkFoldersViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				MainViewEvent.LINK_FOLDERS_VIEW_SHOW,
				MainViewEvent.LINK_FOLDERS_VIEW_HIDE
			];
		}
		
		public function handleLinkFoldersViewShow(data:Object):void
		{
			this.show(data as AppDropboxLinkFoldersVO);
		}
		
		public function handleLinkFoldersViewHide(data:Object):void
		{
			this.hide();
		}
		
		private var fileLoadedCount:int = 0;
		private var linkStatusList:ArrayCollection;
		private var dropboxFileList:ArrayCollection;
		
		public function show(foldersVO:AppDropboxLinkFoldersVO):void
		{
			app.linkFoldersVO = foldersVO;
			
			fileLoadedCount = 0;
			linkStatusList = new ArrayCollection();
			dropboxFileList = new ArrayCollection();
			var clientList:ArrayCollection = new ArrayCollection();
			for each (var itemVO:AppDropboxLinkFolderItemVO in foldersVO.itemList) {
				var dropboxClient:AppDropboxClient = PureMVCHelper.dropboxClientListProxy.getClientByUID(int(itemVO.clientId));
				if (dropboxClient == null) {
					AppTipService.instance.error(AppUtil.getStrStatus("tip.clientNotExists.title"),
						AppUtil.getStrStatus("tip.clientNotExists.text", itemVO.clientId));
				} else {
					dropboxClient.addEventListener(DropboxEvent.METADATA_RESULT, metadataResultHandler);
					dropboxClient.addEventListener(DropboxEvent.METADATA_FAULT, metadataFaultHandler);
					dropboxClient.metadata(foldersVO.folderName, 100, "", true);
					clientList.addItem(dropboxClient);
					
					var linkVO:LinkFoldersStorageStatusVO = new LinkFoldersStorageStatusVO();
					linkVO.client = dropboxClient;
					linkVO.linkItem = itemVO;
					linkStatusList.addItem(linkVO);
				}
			}
			if (linkStatusList.length > 0) {
				LoadingMaskService.instance.show("");
			}
			
			var multiFileListViewMediator:FileListViewMultiClientsMediator = new FileListViewMultiClientsMediator(app.mainFileView);
			multiFileListViewMediator.clientList = clientList;
			this.facade.registerMediator(multiFileListViewMediator);
			
		}
		
		public function hide():void
		{
			this.facade.removeMediator(FileListViewMultiClientsMediator.NAME);
		}
		
		private function metadataResultHandler(event:DropboxEvent):void
		{
			var client:AppDropboxClient = event.currentTarget as AppDropboxClient;
			var parentDropboxFile:AppDropboxFile = new AppDropboxFile(client, event.resultObject as DropboxFile)
			for each (var linkVO:LinkFoldersStorageStatusVO in linkStatusList) {
				if (linkVO.client == client) {
					linkVO.file = parentDropboxFile;
				}
			}
			
			for each (var dropboxFile:DropboxFile in parentDropboxFile.contents) {
				var file:AppDropboxFile = new AppDropboxFile(client, dropboxFile);
				this.dropboxFileList.addItem(file);
			}
			
			if (++fileLoadedCount == linkStatusList.length) {
				metadataLoadComplete();
			}
				
		}
		
		private function metadataFaultHandler(event:DropboxEvent):void
		{
			var client:AppDropboxClient = event.currentTarget as AppDropboxClient;
			if (++fileLoadedCount == linkStatusList.length) {
				metadataLoadComplete();
			}
		}
		
		private function metadataLoadComplete():void
		{
			LoadingMaskService.instance.hide();
			app.linkStatusList = this.linkStatusList;
			app.mainFileView.fileList = this.dropboxFileList;
			app.mainFileView.path = this.app.linkFoldersVO.folderName;
		}
		
		override public function onRegister():void
		{
			app.addEventListener(MainViewEvent.MAX_STORAGE_SIZE_CHANGE, maxStorageSizeChangeHandler);
			app.backButton.addEventListener(MouseEvent.CLICK, backButtonClickHandler);
		}
		
		override public function onRemove():void
		{
			app.removeEventListener(MainViewEvent.MAX_STORAGE_SIZE_CHANGE, maxStorageSizeChangeHandler);
			app.backButton.removeEventListener(MouseEvent.CLICK, backButtonClickHandler);
		}
		
		private function maxStorageSizeChangeHandler(event:MainViewEvent):void
		{
			var storageStatusVO:LinkFoldersStorageStatusVO = event.data;
			var oldValue:Number = event.oldValue;
			var newValue:Number = event.newValue;
			storageStatusVO.linkItem.maxSize = newValue;
			var linkItem:AppDropboxLinkFolderItemVO = PureMVCHelper.dropboxLinkFoldersProxy.getLinkItem(app.folderName, storageStatusVO.client.uid);
			linkItem.maxSize = newValue;
			// let's save when the max storage has been changed
			this.sendNotification(AppDropboxEvent.DROPBOX_STORAGE_SAVE_REQUEST);
		}
		
		private function backButtonClickHandler(event:MouseEvent):void
		{
			this.sendNotification(MainViewEvent.LINK_FOLDERS_VIEW_HIDE);
		}
		
		public function get app():LinkFoldersView
		{
			return viewComponent as LinkFoldersView;
		}
	}
}